/**
 * 
 */
package com.fyp.ptma.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.validator.constraints.Email;

/**
 * @author YahyaArshad
 * 
 */

@Entity
@XmlRootElement(name = "downloader")
@XmlAccessorType(XmlAccessType.NONE)
public class Downloader implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Long id;

	@XmlElement
	private String deviceOs;

	public Downloader(String firstName, String lastName, String email,
			String deviceId, Invitation invitation) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
		this.deviceId = deviceId;
		this.invitation = invitation;
	}

	private String firstName;
	private String lastName;
	@XmlElement(name = "myEmail")
	private String email;
	@XmlElement
	private String googleRegId;
	@XmlElement
	private String deviceId;

	@XmlElement(name = "owner")
	private User user;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(referencedColumnName = "id")
	public User getUser() {
		return user;
	}

	public Downloader() {

	}

	public Downloader(String deviceId) {
		this.deviceId = deviceId;
	}

	public void setUser(User user) {
		this.user = user;
	}

	// Foreign Columns
	private Invitation invitation;

	@XmlElementWrapper(name = "applications")
	@XmlElement(name = "application")
	private List<Application> applications = new ArrayList<Application>();
	private Set<Installation> installations = new HashSet<Installation>();

	/*****************************************************************************************************/

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "downloader")
	@Cascade(value = { CascadeType.SAVE_UPDATE })
	public Set<Installation> getInstallations() {
		return installations;
	}

	public void setInstallations(Set<Installation> installations) {
		this.installations = installations;
	}

	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToMany(mappedBy = "downloaders")
	@Cascade(value = {})
	public List<Application> getApplications() {
		return applications;
	}

	public void setApplications(List<Application> applications) {
		this.applications = applications;
	}

	@OneToOne(mappedBy = "downloader", fetch = FetchType.EAGER)
	@Cascade(value = { CascadeType.ALL })
	public Invitation getInvitation() {
		return invitation;
	}

	public void setInvitation(Invitation invitation) {
		this.invitation = invitation;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Email
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getGoogleRegId() {
		return googleRegId;
	}

	public void setGoogleRegId(String googleRegId) {
		this.googleRegId = googleRegId;
	}

	@Column( unique = true)
	public String getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "DeviceId: " + deviceId + " ### OS: " + deviceOs
				+ " ### Email: " + email;
	}

	@Column(nullable = true, unique = false)
	public String getDeviceOs() {
		return deviceOs;
	}

	public void setDeviceOs(String deviceOs) {
		this.deviceOs = deviceOs;
	}

}
